package org.osami.gateway.watteco;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.logging.Logger;

import org.apache.felix.ipojo.ComponentInstance;
import org.apache.felix.ipojo.Factory;
import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Unbind;
import org.apache.felix.ipojo.annotations.Validate;
import org.osami.gateway.watteco.interfaces.IWattecoSmartplugIPv4;
import org.osgi.framework.ServiceReference;
import org.osgi.service.device.Constants;

@Component
@Instantiate
public class PowerSenseProducerIPv4 extends Thread
{
    private static final Logger logger = Logger.getLogger(PowerSenseProducerIPv4.class.getName());

    private Hashtable<String, IWattecoSmartplugIPv4> devices;

    private volatile boolean quit;
    private final long delay = 1500;
    private final long dutyCycle = 10;


    public PowerSenseProducerIPv4() {
        this.devices = new Hashtable<String, IWattecoSmartplugIPv4>();
    }

    @Validate
    private void startup()
    {
        logger.info("===== Starting Watteco PowerSenseProducerIPv4 =====");
        this.quit = false;

        new Thread(this).start();
    }

    @Invalidate
    private void shutdown()
    {
        logger.info("===== Stopping Watteco PowerSenseProducerIPv4 =====");
        this.quit = true;
    }

    @Bind(aggregate = true)
    public void bindDevice(IWattecoSmartplugIPv4 device, ServiceReference reference) {
        String id = (String) reference.getProperty(Constants.DEVICE_SERIAL);
        logger.info("PowerSenseProducerIPv4: bindDevice" + reference + " for ID " + id);
        devices.put(id, device);
    }

    @Unbind
    public void unbindDevice(ServiceReference reference) {
        String id = (String) reference.getProperty(Constants.DEVICE_SERIAL);
        logger.info("PowerSenseProducerIPv4: unbindDevice" + reference + " for ID " + id);
        devices.remove(id);
    }

    public void run() {
        int i = 1;
        while (!quit)
        {
            if (i % dutyCycle == 0)
            {
                i = 0;
                try {
                    Enumeration<IWattecoSmartplugIPv4> e = devices.elements();
                    while (e.hasMoreElements()) {
                        IWattecoSmartplugIPv4 smartplug = e.nextElement();
                        logger.info("PowerSenseProducerIPv4 calling getMeasurement on " + smartplug);
                        smartplug.getMeasurement();
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }

            try
            {
                Thread.sleep(this.delay);
            }
            catch (InterruptedException e)
            {
            }
            logger.fine("PowerSenseProducerIPv4 waking up " + i);
            i++;
        }
    }
}
